<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>社团</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, user-scalable=no">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta name=”apple-mobile-web-app-status-bar-style” content=”default” />
    <meta name=”apple-mobile-web-app-status-bar-style” content=”black” />
    <meta name=”apple-mobile-web-app-status-bar-style” content=”black-translucent” />
    <meta content="telephone=no" name="format-detection">
    <meta content="email=no" name="format-detection">
    <meta name="format-detection" content="telephone=no"/>

    <link href="${s.base}/mobile/css/weui.min.css" rel="stylesheet" />
    <link href="${s.base}/mobile/css/jquery-weui.css" rel="stylesheet" />
    <link href="${s.base}/mobile/css/swiper.min.css" rel="stylesheet">
    <link href="${s.base}/mobile/css/reset.css" rel="stylesheet" />
    <link href="${s.base}/mobile/css/weChat.css" rel="stylesheet">
</head>
<body>

<div class="weui-pull-to-refresh__layer">
    <div class="pull-to-refresh__arrow"></div> <!-- 上下拉动的时候显示的箭头 -->
    <div class="pull-to-refresh__preloader"></div> <!-- 正在刷新的菊花 -->
    <div class="down">下拉刷新</div><!-- 下拉过程显示的文案 -->
    <div class="up">释放刷新</div><!-- 下拉超过50px显示的文案 -->
    <div class="refresh">正在刷新...</div><!-- 正在刷新时显示的文案 -->
</div>

<div class="mian-ctn">
    <!--tab切换按钮-->
    <div class="flexmenu-line"></div>
    <div class="top-twotab-btn">
        <div class="flexmenutab checked">社团报名</div>
        <div class="flexmenutab">我的社团</div>
    </div>
    <!--tab切换按钮结束-->
    <div class="society-ctn clearfix" id="tab-society">
        <ul id="society-list">

        </ul>
    </div>

    <div class="society-ctn clearfix" id="tab-my-society" style=" display:none">
        <ul id="my-society-list">

        </ul>
    </div>
</div>

<div class="weui-loadmore">
    <i class="weui-loading"></i>
    <span class="weui-loadmore__tips">正在加载</span>
</div>

<div class="nodate-ctn" id='nodate-ctn' style="display: none">
    <p>
        <img src="${s.base}/mobile/images/nodates.png"><br>
        <span>暂无数据</span>
    </p>
</div>


</body>
<script type="text/javascript" src="${s.base}/mobile/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="${s.base}/mobile/js/fontSize.js"></script>
<script type="text/javascript" src="${s.base}/mobile/js/jquery-weui.js"></script>
<script type="text/javascript" src="${s.base}/mobile/js/fastclick.js"></script>
<script type="text/javascript" src="${s.base}/mobile/js/common.js"></script>
<script type="text/javascript">
    var scoietyPage = 1;
    var myScoietyPage = 1;
    var limit = 10;
    var loading = false;
    var checkScoiety = true;
    var scoietyHasMore = true;
    var myScoietyHasMore = true;
    var applyId = '${applyId}';
    var token = '${token}';
    $('.weui-loadmore').hide();

    $(".nodate-ctn").css("height", document.body.scrollHeight - 50);
    $(function () {
        FastClick.attach(document.body);
        initSocietyData('1');
       // initMySocietyData('1');
        initListener();
    });

    function initSocietyData(flag) {
        if (flag === '1') {
            $(document.body).infinite(100);
            scoietyHasMore = true;
        }

        $.fn.doPost('/phone/association/leftJson', {applyId:applyId, pageNum: scoietyPage, pageSize: limit,token:token}, function (result) {
            if (result.code === 0) {
                if (!result.hasNextPage) {
                    scoietyHasMore = false;
                    $(document.body).destroyInfinite();
                }
                if (flag === '1') {
                    $('#society-list').html('');
                    $(document.body).pullToRefreshDone();
                } else if (flag === '2') {
                    loading = false;
                    $(".weui-loadmore").hide();
                }
                postSocietyData(result.data);
            } else {
                $.toast('获取数据失败', "text");
            }
        });

    }

    function initMySocietyData(flag) {
        if (flag === '1') {
            $(document.body).infinite(100);
            myScoietyHasMore = true;
        }

        $.fn.doPost('/phone/association/rightJson', {applyId:applyId, pageNum: myScoietyPage, pageSize: limit,token:token}, function (result) {
            if (result.code === 0) {
                if (!result.hasNextPage) {
                    myScoietyHasMore = false;
                    $(document.body).destroyInfinite();
                }
                if (flag === '1') {
                    $('#my-society-list').html('');
                    $(document.body).pullToRefreshDone();
                } else if (flag === '2') {
                    loading = false;
                    $(".weui-loadmore").hide();
                }
                postMySocietyData(result.data);
            } else {
                $.toast('获取数据失败', "text");
            }
        });

    }

    function initListener() {
        //初始化下拉刷新
        $(document.body).pullToRefresh();
        $(document.body).on("pull-to-refresh", function () {
            if(checkScoiety){
                scoietyPage = 1;
                initSocietyData('1');
            }else{
                myScoietyPage = 1;
                initMySocietyData('1');
            }
        });

        //初始化滚动加载
        $(document.body).infinite().on("infinite", function () {
            if (loading) return;
            loading = true;
            $('.weui-loadmore').show();
            setTimeout(function () {
                if(checkScoiety){
                    scoietyPage++;
                    initSocietyData('2');
                }else{
                    myScoietyPage++;
                    initMySocietyData('2');
                }
            }, 1000);
        });

        $(".flexmenutab").bind("click",function(){
            $(".flexmenutab").removeClass("checked");
            $(this).addClass("checked");
            var tabindex=$(this).index();
            if(tabindex==0){
                $(".flexmenu-line").removeClass("left512").addClass("left138");
                $("#tab-my-society").hide();
                $("#tab-society").show();
                initSocietyData('1');
                if(scoietyHasMore){
                    $(document.body).infinite(100);
                }
            }
            else{
                $(".flexmenu-line").removeClass("left138").addClass("left512");
                $("#tab-society").hide();
                $("#tab-my-society").show();
                initMySocietyData('1');
                if(myScoietyHasMore){
                    $(document.body).infinite(100);
                }
            }
        })
    }

    function postSocietyData(data) {
        var str = '';
        if (data != null && data.length > 0) {
            for (var i = 0; i < data.length; i++) {
                var e = data[i];
                str += '<li>'+
                       '<div class="society-cover" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')"><img src="' + e.path+'">';
                if('1' == e.mystatus){
                   if((e.totalNumber-e.stuNumber)<=0){
                        str +='<div class="bmovered">已招满</div></div>';
                    }else{
                        str +='</div>';
                    }
                }
//            <img src="/sysFile/showFileByPath?path=' + e.path+'">
                else{
                    str +='<div class="bmovered">报名即将开始</div></div>';
                }
                str += '<h1 class="ellipsis" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')">'+e.title+'</h1>'+
                       '<h2 class="ellipsis" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')">'+e.newTitle+'</h2>'+
                       '<div class="instructor ellipsis2" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')"><span>指导老师：</span>'+e.userName+'</div>'+
                       '<div class="hiring" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')"><span>共招'+e.totalNumber+'人</span><span>还剩'+(e.totalNumber-e.stuNumber)+'个名额</span></div>';
                if('1' == e.mystatus){
                    if(e.signId==null||e.signId==""){
                        if((e.totalNumber-e.stuNumber)<=0){
                            str += '<div class="register-now"><img src="${s.base}/mobile/images/bmgrey.png"></div>';
                        }
                        else{
                            str += '<div class="register-now" onclick="associationAdd(\''+e.associationId+'\',\''+e.associationNewId+'\')"><img src="${s.base}/mobile/images/bm.png"></div>';
                        }
                    }else{
                        str += '<div class="register-now" onclick="associationQuit(\''+e.associationId+'\',\''+e.associationNewId+'\')" ><img src="${s.base}/mobile/images/tt.png"></div>'
                    }
                }else{
                    if(e.signId==null||e.signId==""){
                        if((e.totalNumber-e.stuNumber)<=0){
                            str += '<div class="register-now"><img src="${s.base}/mobile/images/bmgrey.png"></div>';
                        }
                        else{
                            str += '<div class="register-now"><img src="${s.base}/mobile/images/bmgrey.png"></div>';
                        }
                    }else{
                        str += '<div class="register-now"><img src="${s.base}/mobile/images/bmed.png"></div>'
                    }
                }
                str += '</li>';
            }
            $('#nodate-ctn').hide();
        }
        if (isBlank(str)) {
            $('#nodate-ctn').show();
            $('#society-list').html(str);
        } else {
            $('#society-list').html(str);
            // $('.question-list ul').append(str);
        }
    }

    function postMySocietyData(data) {
        var str = '';
        if (data != null && data.length > 0) {
            for (var i = 0; i < data.length; i++) {
                var e = data[i];
                str += '<li >'+
                       '<div class="society-cover" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')"><img src="/sysFile/showFileByPath?path=' + e.path+'"></div>'+
                       '<h1 class="ellipsis" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')">'+e.title+'</h1>'+
                       '<h2 class="ellipsis" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')">'+e.newTitle+'</h2>'+
                       '<div class="instructor ellipsis2" onclick="goAssociationDetail(\''+e.associationId+'\',\''+e.associationNewId+'\',1,\''+e.signId+'\')"><span>指导老师：</span>'+e.userName+'</div>';
                if('1' == e.mystatus){
                    str += '<div class="register-now" onclick="associationQuit(\''+e.associationId+'\',\''+e.associationNewId+'\')" ><img src="${s.base}/mobile/images/tt.png"></div>'
                }else {
                    str += '<div class="register-now"><img src="${s.base}/mobile/images/bmed.png"></div>'
                };
                str += '</li>';
            }
            $('#nodate-ctn').hide();
        }
        if (isBlank(str)) {
            $('#nodate-ctn').show();
            $('#my-society-list').html(str);
        } else {
            $('#my-society-list').html(str);
            // $('.question-list ul').append(str);
        }
    }

    function goAssociationDetail(associationId,associationNewId,type,signId){
        window.location.href = "/phone/association/associationDetail?associationId=" + associationId + "&token=${token}&applyId=${applyId}"+"&associationNewId="+associationNewId+"&type="+type+"&signId="+signId;
    }

    //报名
    function associationAdd(associationId,associationNewId){
        $.fn.doPost('/phone/associationSign/wxassociationsign/addOrUpdate', {applyId:applyId,associationId:associationId,associationNewId:associationNewId,token:token}, function (result) {
            if (result.code === 0) {
                $.toast(result.msg, "text");
                initSocietyData('1');
            } else {
                $.toast(result.msg, "text");
            }
        });
    }
    //退团
    //退团
    function associationQuit(associationId,associationNewId) {
        //退团弹窗
        $.confirm("是否确认退团","温馨提示",function () {
            $.fn.doPost('/phone/associationSign/wxassociationsign/quitAssociation', {applyId:applyId,associationId:associationId,associationNewId:associationNewId,token:token}, function (result) {
                if (result.code === 0) {
                    $.toast(result.msg, "text");
                    initSocietyData('1');
                } else {
                    $.toast(result.msg, "text");
                }
            });
        },function () {
        });
    }

    function signUp(associationId,mystatus,status){
        window.location.href = "/phone/question/questionEdit?associationId=" + associationId + "&token=${token}&applyId=${applyId}";
    }
</script>
</html>
